package cn.edu.scau.cmi.huangzhiyuan.db.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * 采用getCurrentSession方法
 * session会在事务完成后自动关闭
 * 要在cfg文件中加入<property name="hibernate.current_session_context_class">thread</property>
 * 
 * @author Yuan
 *
 */
public class HibernateUtil {
	
	private static String configPath = "cn/edu/scau/cmi/huangzhiyuan/db/hibernate.cfg.xml";
	private static SessionFactory sessionFactory;
	
	private HibernateUtil() {
	}
	
	public static Session getSession() {
		return getSessionFactory().getCurrentSession();
	}
	
	public static SessionFactory getSessionFactory() {
		
		if(sessionFactory == null || sessionFactory.isClosed()) {
			sessionFactory = new Configuration().configure(configPath).buildSessionFactory();
		}
		
		return sessionFactory;
	}
	
}
